package activities;

import handler.Utilities;

import java.util.ArrayList;
import java.util.HashMap;

import model.Album;
import model.Artist;
import model.Music;
import model.MusicDB;
import model.Playlist;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.lecteurmp3.R;

/**
 * Class of the activity showing the list of all music
 * @author jean
 *
 */
public class AllMusicActivity extends ListActivity {
	
	public ArrayList<Music> musicList = new ArrayList<Music>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		// Initialization...
		MusicDB musicDb = new MusicDB(this);
		Utilities util = new Utilities();
		musicDb.open();
		
		Album albumCourant;
		Artist artistCourant;
		Playlist playlistCourant;
		
		Bundle extras = getIntent().getExtras();
		
		if(extras != null) {
			if(extras.containsKey("albumId")) {
				albumCourant = musicDb.cursorToAlbum(musicDb.getAlbum(extras.getInt("albumId"))).get(0);
				musicList = musicDb.cursorToMusic(musicDb.getMusicByAlbum(albumCourant));
			} else if(extras.containsKey("artistId")) {
				artistCourant = musicDb.cursorToArtist(musicDb.getArtist(extras.getInt("artistId"))).get(0);
				musicList = musicDb.cursorToMusic(musicDb.getMusicByArtist(artistCourant));
			} else if(extras.containsKey("playlistId")) {
				playlistCourant = musicDb.cursorToPlaylist(musicDb.getPlaylist(extras.getInt("playlistId"))).get(0);
				musicList = musicDb.cursorToMusic(musicDb.getMusicByPlaylist(playlistCourant));
			} else {
				musicList = musicDb.cursorToMusic(musicDb.getAllMusic());
			}
		} else {
			musicList = musicDb.cursorToMusic(musicDb.getAllMusic());
		}
		ArrayList<HashMap<String, String>> musicListView = new ArrayList<HashMap<String,String>>();
		
		for(int i=0;i<musicList.size();i++) {
			Music m = musicList.get(i);
			Artist ar = musicDb.cursorToArtist(musicDb.getArtist(m.getIdArtist())).get(0);
			Album al = musicDb.cursorToAlbum(musicDb.getAlbum(m.getIdAlbum())).get(0);
			musicListView.add(util.MusicToHashMap(m, ar, al));
		}
		musicDb.close();
		
		// Add items to ListView
		ListAdapter adapter = new SimpleAdapter(this, musicListView,
												R.layout.list_item,
												new String[] { "title", "artist"},
												new int[] {R.id.titleItem, R.id.subTitleItem });

		setListAdapter(adapter);
		
		// selecting single ListView item
		ListView lv = getListView();
		// listening to single listitem click
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				// Starting new intent
				Bundle bundle = new Bundle();  
				bundle.putParcelableArrayList("songsList", musicList);
				bundle.putInt("songIndex", position);
				getParent().getIntent().putExtras(bundle);
				finish();
			}
		});
    }

}
